package jsyf.oa.web.email;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.email.DraftFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by y on 2018/3/7.
 */
@Api(value = "草稿箱接口", description = "草稿箱接口")
@Controller
@RequestMapping("/draft")
public class DraftController {
    @Resource
    private DraftFacade draftFacade;
    @ResponseBody
    @RequestMapping(value = "/getDraftsByCreateUserId.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询所有邮件",
            notes = "查询所有邮件 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'userId':'当前用户','requestPage':'请求页面','pageSize':'分页大小'}", dataType = "String", paramType = "body")
    public void getEmailsByCreateUserId(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData){

        JSONObject records = this.draftFacade.findDraftList(jsonData);
        if (records!= null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }
    @ResponseBody
    @RequestMapping(value = "/editDraftById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "编辑邮件",
            notes = "编辑邮件 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'draft':[{'id':'草稿Id'}]", dataType = "String", paramType = "body")
    public void editDraftById(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData){
        JSONObject records = this.draftFacade.findDraftDetail(jsonData);
        if (records!= null) {
            ResponseUtils.putJsonResponse(response,BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }
    @ResponseBody
    @RequestMapping(value = "/delDraftById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "删除草稿",
            notes = "删除草稿 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'draft':[{'id':'草稿Id'}]", dataType = "String", paramType = "body")
    public void delDraftById(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData){
        Boolean records = this.draftFacade.removeById(jsonData);
        ResponseUtils.putJsonResponse(response,BaseUtils.succsessObjRespond(records.toString()));
    }
}
